package cn.txun05.csmall.product.controller;
import cn.txun05.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.txun05.csmall.product.pojo.vo.AlbumListItemVo;
import cn.txun05.csmall.product.pojo.vo.AlbumStandardVo;
import cn.txun05.csmall.commons.pojo.vo.PageData;
import cn.txun05.csmall.commons.security.LoginPrincipal;
import cn.txun05.csmall.commons.web.JsonResult;
import cn.txun05.csmall.product.pojo.param.AlbumAddNewParam;
import cn.txun05.csmall.product.service.IAlbumService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping(value = "/albums", produces = "application/json; charset=utf-8")
@Api(tags = "04.相册管理模块")
@Slf4j
@Validated
public class AlbumController{
    @Resource
    @Autowired(required = true)
    private IAlbumService albumService;

    AlbumController(){
        log.debug("AlbumController()");
    }


    //http://localhost:8080/album/add-new?name=testname&description=hahha&sort=99
    //@RequestMapping(value = "/add-new",method = {RequestMethod.POST,RequestMethod.GET})
    @PostMapping("add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AlbumAddNewParam albumAddNewParam){
            log.debug("开始处理【添加相册】请求，参数：{}",albumAddNewParam);
            albumService.addNew(albumAddNewParam);
            log.debug("处理【添加相册】的请求，完成！");
            //return "添加成功";

//            JsonResult jsonResult = new JsonResult();
//            jsonResult.setState(1);
//            jsonResult.setMessage("添加成功");
            //return new JsonResult(1,"添加成功");
            //return new JsonResult().setState(1).setMessage("添加成功");
        return JsonResult.ok();
    }
    //localhost:9180/album/delete?albumId=9527
    //localhost:9180/album/9527/delete
    @PostMapping("/{albumId:[0-9]+}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name ="albumId" ,value = "相册Id",required = true,dataType = "long")
           })

    public JsonResult delete(@Range(min = 1,message = "根据Id删除相册失败，请提交合法的Id的值！")
                                @PathVariable("albumId") Long albumId){

       log.debug("开始处理【根据ID删除相册】的请求，参数：{}",albumId);
       albumService.delete(albumId);
       return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id" ,value = "相册Id",required = true,dataType = "long")
    })
    public JsonResult updateInfoById(
            @PathVariable("id")
            @Range(min=1,message = "请提交有效的相册id值！") Long id,
            @Valid AlbumUpdateInfoParam albumUpdateInfoParam){
        log.debug("开始处理【修改相册详情】的业务，参数：{}",albumUpdateInfoParam);
        albumService.updateInfoById(id,albumUpdateInfoParam);
        return JsonResult.ok();
    }
    //localhost:9180/albums
    @GetMapping("")
    @PreAuthorize("hasAuthority('/pms/album/read')")
    @ApiOperation("查询相册")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name ="pageNum" ,value = "页码",paramType = "query")
    })
    public JsonResult list(@Range(min = 1,message = "查询相册列表失败，请提供正确的页码值！")
                                       Integer pageNum,
           @ApiIgnore  @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【查询相册列表】的请求，页码：{}",pageNum);
        log.debug("当事人id:{}",loginPrincipal.getId());
        log.debug("当事人username:{}",loginPrincipal.getUsername());
        if(pageNum==null || pageNum <1){
            pageNum=1;
        }
        PageData<AlbumListItemVo> pageData =  albumService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id" ,value = "相册ID",required = true,dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1,
            message = "根据ID查询相册详情失败，请提供正确的id值！")
                                  @PathVariable Long id){
        log.debug("开始处理【根据ID查询相册详情】的请求，参数：{}",id);
        AlbumStandardVo result =  albumService.getStandardById(id);
        return JsonResult.ok(result);
    }
}
